package com.be.beadmin.material.controller;

import com.be.beadmin.material.dto.output.MaterialShortageExcelEx;
import com.be.beadmin.material.dto.output.PickingTaskExcelEx;
import com.be.beadmin.material.entity.MaterialShortage;
import com.be.beadmin.material.dto.output.MaterialShortageEx;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;
import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.material.service.MaterialShortageService;
import com.be.beadmin.material.dto.input.MaterialShortageQueryCriteria;
import com.be.beadmin.material.dto.input.MaterialShortageDto;
import org.springframework.data.domain.Pageable;
import com.be.beadmin.common.utils.ResultModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;


import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

import com.alibaba.excel.EasyExcel;

/**
* @author zmy
* @date 2023-07-17
**/
@RestController
public class MaterialShortageController {

    @Autowired
    private  MaterialShortageService materialShortageService;


    @PostMapping(value = "/material/getMaterialShortageByPage")
    public ResultModel getMaterialShortageByPage(@RequestBody PageEntity pageEntity)  {
        return ResultModel.success(materialShortageService.getMaterialShortageByPage(pageEntity)) ;
    }

    @PostMapping("/material/addMaterialShortage")
    public ResultModel addMaterialShortage(@RequestBody MaterialShortageDto materialShortageDto){
        materialShortageService.addMaterialShortage(materialShortageDto);
        return ResultModel.success(ResultModel.ADD);
    }

    @PostMapping("/material/delMaterialShortage")
    public ResultModel delMaterialShortage(@RequestBody MaterialShortageDto materialShortageDto){
        materialShortageService.delMaterialShortage(materialShortageDto.getId());
        return ResultModel.success(ResultModel.DELETE);
    }


    @PostMapping("/material/updateMaterialShortage")
    public ResultModel updateMaterialShortage(@RequestBody MaterialShortageDto materialShortageDto){
        materialShortageService.updateMaterialShortage(materialShortageDto);
        return ResultModel.success(ResultModel.UPDATE);
    }


    @PostMapping("/material/exportMaterialShortage")
    public void exportMaterialShortage(HttpServletResponse response ,@RequestBody PageEntity pageEntity) throws IOException {
        Page<MaterialShortageEx> materialShortagePage = materialShortageService.getMaterialShortageByPage(pageEntity);
        List<MaterialShortageEx> materialShortageExes = materialShortagePage.getContent();
        List<MaterialShortageExcelEx> materialShortageExcelExes=materialShortageExes.stream().map(item->{
            MaterialShortageExcelEx excel=new MaterialShortageExcelEx();
            BeanUtils.copyProperties(item,excel);
            return excel;
        }).collect(Collectors.toList());
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("MaterialShortage接口", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), MaterialShortageExcelEx.class).sheet("sheet1").doWrite(materialShortageExcelExes);
    }
}